<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use App\Repository\Repositories\Interfaces\ReceivingAccountRepository;

 
class ReceivingAccountController extends BaseController
{
   
    protected $receiving_account_repo;
    protected $shop_id;
    protected $user_id;
    
    public function __construct(ReceivingAccountRepository $receiving_account_repo)
    {
        parent::__construct();
        $this->receiving_account_repo = $receiving_account_repo;
        //用户只能操作自身所属数据
  
        $this->shop_id =isset($this->user) &&  isset($this->user->shop_id) ? $this->user->shop_id : 0;
        $this->user_id =isset($this->user) &&  isset($this->user->id) ? $this->user->id : 0;
        
        $this->receiving_account_repo->scopeQuery(function ($query) {
            return $query->where(['shop_id'=> $this->shop_id,'user_id'=>$this->user_id]);
        });
    }

    public function index()
    {
        $orders = $this->receiving_account_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $orders, true);
    }

    public function show($id)
    {
        $product = $this->receiving_account_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }
    
    public function default($id)
    {
        $product = $this->receiving_account_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }

    public function store(Request $request)
    { 
        $this->validate($request,[
            //'czbank_no'=> 'required|string',
            'bank_name'=> 'required|string',
            'bank_no'=> 'required|string',
            //'account_icon'=> 'required|string',
            'account_type'=> 'required|string',
            //'cert_no'=>'required|string',
            'account_name'=>'required|string',
        ]);
        
        $data = $request->all();
        
        $data['shop_id'] = $this->shop_id;
        $data['user_id'] = $this->user_id;
        
        $ret = $this->receiving_account_repo->getLastId();
        if(isset($ret['data']['id']) && $ret['data']['id'] > 0){
            $data['account_id'] = "8".date('YmdH') . rand(1000000,9999999).str_pad($ret['data']['id'], 5, '0', STR_PAD_LEFT);
        }else{
            $data['account_id'] = "8".date('YmdH') . rand(1000000,9999999).str_pad(1, 5, '0', STR_PAD_LEFT);
        }
        
        if($data['accout_status']  == 1){
            $ret = $this->receiving_account_repo->defaultAccount($this->shop_id);
            if(isset($ret['data']) && count($ret['data']) > 0){
                $data['is_default'] = 0;
            }else{
                $data['is_default'] = 1;
            }
        }else{
            $data['is_default'] = 0;
        }
       
 
        $ret = $this->receiving_account_repo->create($data);
        
        return $this->noDataReturn($ret ? true : false);
    }

    public function update(Request $request, $id)
    { 
        $this->validate($request,[
            //'czbank_no'=> 'required|string',
            'bank_name'=> 'required|string',
            'bank_no'=> 'required|string',
            //'account_icon'=> 'required|string',
            'account_type'=> 'required|string',
            //'cert_no'=>'required|string',
            'account_name'=>'required|string',
        ]);
        
        $data = $request->all();
      
        
        if($data['accout_status']  == 1){
            $ret = $this->receiving_account_repo->defaultAccount($this->shop_id);
            if(isset($ret['data']) && count($ret['data']) > 0){
                $data['is_default'] = 0;
            }else{
                $data['is_default'] = 1;
            }
        }else{
            $data['is_default'] = 0;
        }
        
        $ret = $this->receiving_account_repo->update($data,$id);
        
        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id)
    {
        $ret = $this->receiving_account_repo->delete($id);
        return $this->noDataReturn($ret ? true : false);
    }
    
    public function updateDefault(Request $request){
        $this->validate($request,[
            'id'=>'required|integer',
            'is_default'=>'required|integer',
        ]);
        $data = $request->all(['is_default','id']);
        $data['shop_id']=$this->shop_id;
  
        $ret = $this->receiving_account_repo->updateDefault($data);
       
        return $this->noDataReturn($ret ? true : false);
    }
    
  
    public function getDefaultAccount(){
        $ret = $this->receiving_account_repo->defaultAccount($this->shop_id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $ret['data']);
    }
}
